/*  This file is part of CASpp.

    CASpp is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    CASpp is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with CASpp.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2014 Jérome LAURENS

    See https://code.google.com/p/idalgo/

*/

#include "function.h"
#include "../node.h"
#include <sstream>
#include <boost/foreach.hpp>
#include <boost/algorithm/string/join.hpp>

CASPP_NS_BEGIN

Function::Function(const name_t & n)
        : symbol_m(SymbolRef(new Symbol(n))) {
}
Function::Function(const Symbol & n)
    : symbol_m(SymbolRef(new Symbol(n))) {
}
Function::Function(const SymbolRef & n)
    : symbol_m(n) {
}
Function::Function(const SymbolRef &n, const ListRef &l)
    : symbol_m(n)
    , arguments_m(l) {
}

const Function::name_t Function::name() const {
    return symbol_m.get()? symbol_m->name(): "None";
}
const SymbolRef &Function::symbolRef() const {
    return symbol_m;
}
SymbolRef &Function::symbolRef() {
    return symbol_m;
}
const ListRef &Function::argumentsRef() const {
    return arguments_m;
}
ListRef & Function::argumentsRef() {
    return arguments_m;
}
const std::string Function::description() const {
    return name()+"(...)";
}
const std::string Function::longDescription() const {
    std::vector< std::string > list;
    std::ostringstream s;
    s << "Function ";
    if (symbol_m.get()!=0) {
        s << symbol_m->description();
    } else {
        s << "None";
    }
    s << " (...";
    list.push_back(s.str());
    if (arguments_m.get()!=0) {
        list.push_back(arguments_m->description());
    }
    list.push_back("...)");
    return boost::algorithm::join(list, "\n");
}

CASPP_NS_END
